//
//  ST_BTVideoBarrageAdapter.swift
//  btvideo
//
//  Created by mac on 2020/6/10.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyJSON
import SwifterSwift
import HandyJSON

class ST_BTVideoBarrageAdapter: NSObject {

//    MARK: - shared
    static let shared = ST_BTVideoBarrageAdapter()
    
    private override init() {
        super.init()
        
//        print("iOS 所有字体:",UIFont.familyNames)
    }
    
    private var barrage_caches = [String:[ST_CommentTableCellModel]]()
    
//    MARK: - private method
    
 
//    MARK: - public method
    func fetch_barrages(groupId:String,success:@escaping ([ST_FWBarrageItemModel],Int)->()){
        
        /*
        if let list = barrage_caches[groupId]{
            
            let array = generate_barrage_item(list: list)
            
            
            success(array)
            
        }else{
            
            */
            
            JVrequest(PH.queryPecCommentsApi,parameters: ["group_id":groupId], success: { (json) in
                
                let data = json["data"]
//                print("弹幕 打印:",data)
                
                if let list = [ST_CommentTableCellModel].deserialize(from: data["list"].arrayObject){
                    
                    let existArray = list.compactMap({$0})
                                    
                    existArray.forEach { (cellModel) in
                        
                        cellModel.setUpDataSet()
                        
                        let str = cellModel.comment
                        
                        if str.count > 50{
                            let index = str.index(str.startIndex, offsetBy: 50)
                            let mySubstring = str[..<index]
                            
                           cellModel.comment = String(mySubstring)
                        }
                    }
                    
                    
//                    self.barrage_caches.updateValue(existArray, forKey: groupId)
                    
                    let array = self.generate_barrage_item(list: existArray)
                    
                    success(array,data["total"].intValue)
                    
                }
                
            }) { (code, err) in
                
                print("弹幕 打印:",err)
                
            }
        
//    }
        
    }
    
    
    
    
    private func generate_barrage_item(list:[ST_CommentTableCellModel])->[ST_FWBarrageItemModel]{
        
        var data_array = [ST_FWBarrageItemModel]()
                            
        for item in list {
        
            
            let bm = ST_BarrageModel()
            
            bm.udid = item.comment_id.string
            
            bm.reply_content = item.comment
            //    16进制 ex: FFFFFF
            bm.reply_color = "000000"
            
            if item.is_anonymous == 1{
               
                bm.is_vip = false
                
            }else{
                if CHECK_MEMBER_STATUS(expirationTime: item.vip_expire) == .subscribe{
                    bm.is_vip = true
                }else{
                    bm.is_vip = false
                }
            }
            
            bm.avatar = item.avatar
            
            let m = ST_CompatibilityBarrageItem(item: bm)
            
            data_array.append(m)
        }
        
        return data_array
    }
    
    
    
}

